<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前端购物车类
 * @package  module/base/shop
 * @author   allen.qiang
 * @version 1.0
 */

class shopCart {

    /**
     * 购物车数组结构
     * @return array
     */
    private function createCartStructure() {

        $bill_array = array(

            'bill_no' => null,

            'bill_status' => 0,

            'change_time' => null,

            'message' => null,

            'goods' => array(),

            'master' => array()

        );

        return $bill_array;

    }

    /**
     * 生成购物车商品数组结构
     * @param array $goods_array 商品数组
     * @return array
     */
    
    public function createCartGoodsStructure( $goods_array ) {

        $goods_array['addPoints'] = 0;
        $goods_array['salePoints'] = 0;
        $goods_array['goods_from'] = 1;
        $goods_array['goods_groupID'] = 0;
        $goods_array['price'] = $goods_array['salePrice'];
        $sync_array = array(); //切分数组
        $birth_array = array(); //生日牌数组

        if ($goods_array['syncID']) { //切分
            $sync_id_array = explode('|', $goods_array['syncID']);
            $syncopate_array = explode('|', $goods_array['syncopate']);
            foreach($sync_id_array as $k => $v) {
                $sync_array[$v] = $syncopate_array[$k];
            }
        }

        if ($goods_array['birthID']) { //生日牌
            $birth_id_array = explode('|', $goods_array['birthID']);
            $birthcard_array = explode('|', $goods_array['birthcard']);
            foreach($birth_id_array as $k => $v) {
                $birth_array[$v] = $birthcard_array[$k];
            }
        }

        return array (
            'goodsID' => $goods_array['goodsID'],
            'postID' => $goods_array['postID'],
            'goodsName' => $goods_array['goodsName'],
            'frenchName' => $goods_array['frenchName'],
            'spec' => $goods_array['spec'],
            'goods_amount' => $goods_array['goods_amount'],
            'salePrice' => $goods_array['salePrice'],
            'price' => $goods_array['price'],
            'addPoints' => $goods_array['addPoints'],
            'salePoints' => $goods_array['salePoints'],
            'goods_from' => $goods_array['goods_from'],
            'goods_groupID' => $goods_array['goods_groupID'],
            'goods_status' => 1,
            'is_buy' => 1,      //是否购买：0、不购买    1、暂时购买
            'img_link' => $goods_array['img_link'],
            'link' => $goods_array['link'],
            'wap_link' => $goods_array['wap_link'],
            'bigCategoryID' => $goods_array['bigCategoryID'],
            'midCategoryID' => $goods_array['midCategoryID'],
            'smallCategoryID' => $goods_array['smallCategoryID'],
            'bigCategoryName' => $goods_array['bigCategoryName'],
            'midCategoryName' => $goods_array['midCategoryName'],
            'smallCategoryName' => $goods_array['smallCategoryName'],
            'product_code' => $goods_array['product_code'],
            'twoPostID' => $goods_array['twoPostID'],
            'edible' => $goods_array['edible'],
            'fittings' => $goods_array['fittings'],
            'startDeliver' => $goods_array['startDeliver'],
            'endDeliver' => $goods_array['endDeliver'],
            'deliverSpace' => $goods_array['deliverSpace'],
            'cutTime' => $goods_array['cutTime'],
            'choose_sync_id' => 0,
            'choose_birth_id' => 0,
            'cityID' => $goods_array['cityID'],
            'sync_array' => $sync_array,
            'birth_array' => $birth_array,
        );

    }

    /**
     * 得到购物车数组
     * @param string $cart_name
     * @return mixed
     */
    public function getCartInfo( $cart_name = 'shop_cart' ) {

        global $G_SHOP;

        $cart_name .= $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        $bill_array = $G_SHOP->sessionCache_get($cart_name);

        if (!$bill_array)
            $bill_array = $this->createCartStructure();

        return $bill_array;

    }

    /**
     * 保存购物车数组
     * @param $bill_array
     * @param string $cart_name
     */
    public function setCartInfo( $bill_array, $cart_name = 'shop_cart' ) {

        global $G_SHOP;

        $cart_name .= $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        $G_SHOP->sessionCache_set($cart_name, $bill_array, G_SHOPPINGCART_TIMEOUT);

    }

    public function removeCartInfo( $cart_name = 'shop_cart' ) {

        global $G_SHOP;

        $cart_name .= $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        $G_SHOP->sessionCache_remove($cart_name);

    }

    /**
     * 取得购物车商品数量
     * @return int
     */
    public function getCartGoodsAmount() {

        global $G_SHOP;

        $cart_amount = $G_SHOP->get_cookie_info( 'cart_amount' );

        return $cart_amount ? intval($cart_amount) : 0;

    }

    /**
     * 更新购物车商品数量
     * @param int $goods_amount 商品数量
     */
    public function updateCartGoodsAmount( $goods_amount ) {

        global $G_SHOP;

        $G_SHOP->set_cookie_info('cart_amount', $goods_amount, G_SHOPPINGCART_TIMEOUT * 1000);

    }

    /**
     * 商品加入购物车
     * @param int $postID 商品网购号
     * @param int $goods_amount 商品数量
     * @return array
     */
    public function addGeneralGoods( $postID, $goods_amount ) {

        global $G_SHOP;

        //加载类文件
        quanli_include('module/base/goods/quanliGoods.class.php');
        quanli_include('module/base/goods/goodsFittings.class.php');

        //实例商品配件类
        $goodsFittings = new goodsFittings( $G_SHOP->DBCA );

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        //购物车信息
        $bill_array = $this->getCartInfo();
        $cart_array =& $bill_array['goods'];

        if (isset($cart_array['general'][$postID])) {

            $cart_array['general'][$postID]['goods_amount'] += $goods_amount;

        } else {

            //查询商品信息
            $arg_array = array(
                'postID' => $postID
            );
            $goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );
            if (!$goods_array) {
                return array( 'code' => 404, 'message' => '商品信息没有找到' );
            }
            if ($goods_array['cityID'] != $city_id) {
                return array( 'code' => 404, 'message' => '此款产品在当前地区暂不支持配送' );
            }
            if ($goods_array['useFlg'] == 0 || $goods_array['useFlg'] == 2 || $goods_array['useFlg'] == 3) {
                return array( 'code' => 404, 'message' => '商品已下架或售罄' );
            }

            //查询商品配件
            $arg_array = array(
                'postID' => $postID
            );
            $goods_fittings_array = $goodsFittings->searchGoodsFitsRelation( $arg_array );

            //购物车商品数组结构
            $goods_array['goods_amount'] = $goods_amount;
            $goods_array['fittings'] = $goods_fittings_array;
            $cart_goods_array = $this->createCartGoodsStructure( $goods_array);
            $cart_array['general'][$postID] = $cart_goods_array;

        }

        //保存购物车信息
        $bill_array['change_time'] = time();
        $this->setCartInfo( $bill_array );

        //更新购物车数量
        $cart_goods_amount = $this->getCartGoodsAmount();
        $cart_goods_amount += $goods_amount;
        $this->updateCartGoodsAmount( $cart_goods_amount );

        $data_array = array(
            'postID' => $cart_array['general'][$postID]['postID'],
            'product_code' => $cart_array['general'][$postID]['product_code'],
            'goodsName' => $cart_array['general'][$postID]['goodsName'],
            'price' => $cart_array['general'][$postID]['price'],
            'goods_amount' => $goods_amount,
            'bigCategoryID' => $cart_array['general'][$postID]['bigCategoryID'],
            'bigCategoryName' => $cart_array['general'][$postID]['bigCategoryName']
        );

        return array( 'code' => 200, 'message' => 'ok', 'data' => $data_array );

    }

    /**
     * 更新商品切分
     * @param int $sync_id 切分数量
     * @param int $index 商品索引
     * @return array
     */
    public function segmentation( $sync_id, $index ) {

        //购物车信息
        $bill_array = $this->getCartInfo();

        //检查商品是否在购物车里
        if (!isset($bill_array['goods']['general'][$index])) {
            return array( 'code' => 400, 'message' => '商品信息没有找到' );
        }

        //更新商品切分信息
        $goods_array =& $bill_array['goods']['general'][$index];
        if (isset($goods_array['sync_array'][$sync_id])) {
            $goods_array['choose_sync_id'] = $sync_id;
        } else {
            $goods_array['choose_sync_id'] = 0;
        }

        $this->setCartInfo( $bill_array );

        return array( 'code' => 200, 'message' => 'ok' );

    }

    /**
     * 更新普通商品购物车商品数量
     * @param int $index 商品索引
     * @param int $goods_amount 商品数量
     * @return array
     */
    public function updateGeneralGoods( $index, $goods_amount ) {

        //购物车信息
        $bill_array = $this->getCartInfo();

        //检查商品是否在购物车里
        if (!isset($bill_array['goods']['general'][$index])) {
            return array( 'code' => 400, 'message' => '商品信息没有找到' );
        }
        $cart_goods_array = &$bill_array['goods']['general'][$index];

        //更新普通商品购物车商品数量
        $cart_goods_array['goods_amount'] = $goods_amount;

        $this->setCartInfo( $bill_array );

        $data_array = array(
            'postID' => $cart_goods_array['postID'],
            'product_code' => $cart_goods_array['product_code'],
            'goodsName' => $cart_goods_array['goodsName'],
            'price' => $cart_goods_array['price'],
            'goods_amount' => $goods_amount,
            'bigCategoryID' => $cart_goods_array['bigCategoryID'],
            'bigCategoryName' => $cart_goods_array['bigCategoryName']
        );

        return array( 'code' => 200, 'message' => '商品更新成功', 'data' => $data_array );

    }

    /**
     * 删除购物车商品
     * @param int $index 商品索引
     * @return array
     */
    public function delete( $index ) {

        //购物车信息
        $bill_array = $this->getCartInfo();

        //检查商品是否在购物车里
        if (!isset($bill_array['goods']['general'][$index])) {
            return array( 'code' => 400, 'message' => '商品信息没有找到' );
        }

        $data_array = array(
            'postID' => $bill_array['goods']['general'][$index]['postID'],
            'product_code' => $bill_array['goods']['general'][$index]['product_code'],
            'goodsName' => $bill_array['goods']['general'][$index]['goodsName'],
            'price' => $bill_array['goods']['general'][$index]['price'],
            'goods_amount' => $bill_array['goods']['general'][$index]['goods_amount'],
            'bigCategoryID' => $bill_array['goods']['general'][$index]['bigCategoryID'],
            'bigCategoryName' => $bill_array['goods']['general'][$index]['bigCategoryName']
        );

        //删除购物车商品
        unset($bill_array['goods']['general'][$index]);

        $this->setCartInfo( $bill_array );

        return array( 'code' => 200, 'message' => '商品删除成功', 'data' => $data_array );

    }

    /**
     * 加载购物车数据
     * @return array
     */
    public function loadCart() {

        global $G_SHOP;

        quanli_include('module/base/customer/quanliCust.class.php');

        $shop_array = array(
            'total_money' => 0, //商品总金额
            'total_amount' => 0, //商品总数量
            'is_buy_total_money' => 0, //暂时购买商品总金额
            'is_buy_total_amount' => 0 //暂时购买商品总数量
        );

        //实例会员类
        $quanliCust = new quanliCust();

        //获得会员等级信息
        $cust_rank_array = $quanliCust->getCustRank( $G_SHOP->currentPCustID );

        //购物车数组
        $bill_array = $this->getCartInfo();
        $cart_array = &$bill_array['goods'];

        /**
         * ===========================================================
         * 普通商品
         * ===========================================================
         */
        if (isset($cart_array['general'])) {

            foreach ($cart_array['general'] as &$list) {

                $list['price'] = round($list['salePrice'] * $cust_rank_array['rate'], 2);
                $shop_array['total_money'] += $list['price'] * $list['goods_amount'];
                $shop_array['total_amount'] += $list['goods_amount'];

                //筛选暂时购买商品
                if(isset($list['is_buy']) && $list['is_buy'] == 0)
                    continue;

                $shop_array['is_buy_total_money'] += $list['price'] * $list['goods_amount'];
                $shop_array['is_buy_total_amount'] += $list['goods_amount'];

            }

            $shop_array['goods'] = $cart_array['general'];

        }

        //更新购物车信息
        $bill_array['change_time'] = time();
        $this->setCartInfo($bill_array);

        //更新购物车商品数量
        $this->updateCartGoodsAmount( $shop_array['total_amount'] );

        return $shop_array;

    }

    public function emptyCart($cart_name = 'shop_cart') {

        $bill_array = $this->getCartInfo($cart_name);

        $bill_array['goods'] = array();

        //更新购物车信息
        $bill_array['change_time'] = time();
        $this->setCartInfo( $bill_array,$cart_name );

        //更新购物车数量
        $this->updateCartGoodsAmount( 0 );

        return array( 'code' => 200, 'message' => 'ok' );

    }

    public function nowBuy($postID, $goods_amount) {

        global $G_SHOP;

        //加载类文件
        quanli_include('module/base/goods/quanliGoods.class.php');
        quanli_include('module/base/goods/goodsFittings.class.php');
        //加载活动配置类 add by allen.qiang 2015-05-15
        quanli_require('module/base/activity/activityConfig.class.php');

        //实例商品配件类
        $goodsFittings = new goodsFittings( $G_SHOP->DBCA );

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        //购物车信息
        $bill_array = $this->getCartInfo('now_buy');
        $cart_array =& $bill_array['goods'];
        $cart_array['general'] = array();

        //查询商品信息
        $arg_array = array(
            'postID' => $postID
        );
        $goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );
        if (!$goods_array) {
            return array( 'code' => 404, 'message' => '商品信息没有找到' );
        }
        if ($goods_array['cityID'] != $city_id) {
            return array( 'code' => 404, 'message' => '此款产品在当前地区暂不支持配送' );
        }
        if ($goods_array['useFlg'] == 0 || $goods_array['useFlg'] == 2 || $goods_array['useFlg'] == 3) {
            return array( 'code' => 404, 'message' => '商品已下架或售罄' );
        }

        //加载系统全局设置信息 add by allen.qiang 2015-05-15 Start

        //实例化系统配置类
        $systemConfig = new activityConfig('systemConfig');
        $config = $systemConfig->config;

        //限购数量
        if(isset($config['goods_limit_num']) && !empty($config['goods_limit_num'])){

            $goods_limit = (array)json_decode($config['goods_limit_num']);
            $goods_limit_txt = (array)json_decode($config['goods_limit_txt']);

            $goodsLimit = $goodsLimitTxt = array();

            foreach($goods_limit as $procut_code => $num){

                $result = $G_SHOP->DBCA->stmt_query("SELECT postID FROM goods_sale WHERE product_code = '{$procut_code}'");

                foreach($result as $row){

                    $goodsLimit[$row->postID] = $num;

                    if(isset($goods_limit_txt[$procut_code])){
                        $goodsLimitTxt[$row->postID] = $goods_limit_txt[$procut_code];
                    }
                }
            }

            //判断限购数量

            if(isset($goodsLimit[$postID]) && $goods_amount>$goodsLimit[$postID]){
                $msg = isset($goodsLimitTxt[$postID])?$goodsLimitTxt[$postID]:'所选商品购买数量上限个数为'. $goodsLimit[$postID] .',请重新选择此商品或再次选择其它商品';
                return array( 'code' => 400, 'message' => $msg);
            }

        }

        //加载系统全局设置信息 add by allen.qiang 2015-05-15 End

        //查询商品配件
        $arg_array = array(
            'postID' => $postID
        );
        $goods_fittings_array = $goodsFittings->searchGoodsFitsRelation( $arg_array );

        //购物车商品数组结构
        $goods_array['goods_amount'] = $goods_amount;
        $goods_array['fittings'] = $goods_fittings_array;
        $cart_goods_array = $this->createCartGoodsStructure( $goods_array);
        $cart_array['general'][$postID] = $cart_goods_array;

        //保存购物车信息
        $this->setCartInfo( $bill_array, 'now_buy' );

        $data_array = array(
            'postID' => $cart_array['general'][$postID]['postID'],
            'product_code' => $cart_array['general'][$postID]['product_code'],
            'goodsName' => $cart_array['general'][$postID]['goodsName'],
            'price' => $cart_array['general'][$postID]['price'],
            'goods_amount' => $goods_amount,
            'bigCategoryID' => $cart_array['general'][$postID]['bigCategoryID'],
            'bigCategoryName' => $cart_array['general'][$postID]['bigCategoryName']
        );

        return array( 'code' => 200, 'message' => 'ok', 'data' => $data_array );

    }

    /**
     * 官网活动购物车
     * $postID 商品网购DI，$goods_amount 商品数量，$type 购物车名称类型
     * @return $activity_array
     * @author  20141219 allen.qiang <allen.qiang@fistforward.com>
     */
    public function nowBuy_activity($postID, $goods_amount,$type) {

        global $G_SHOP;

        //加载类文件
        quanli_include('module/base/goods/quanliGoods.class.php');
        quanli_include('module/base/goods/goodsFittings.class.php');
        quanli_require('module/base/activity/activitySpike.class.php');

        //add 20141222 diandong.cao FUN-AC-0031-BD圣诞节 start
        quanli_require('module/base/activity/activityConfig.class.php');//活动配置文件
        $activity_config = new activityConfig('christmas_cake'); //活动配置
        $config_array = $activity_config->config;
        //add 20141222 diandong.cao FUN-AC-0031-BD圣诞节 end

        //add by 20141120 allen.qiang  FUN-AC-0020-BD建设银行五折购 ---start
        quanli_require('module/base/activity/activityCbc.class.php');
        //实例建设银行五折购活动类
        $activityCBC=new activityCbc();
        $_REQUEST["pcustID"]=$G_SHOP->currentPCustID;
        $activity_master=$activityCBC->activity_master($_REQUEST);
        //add by 20141120 allen.qiang  FUN-AC-0020-BD建设银行五折购 ---end

        //add by 20141219 allen.qiang  FUN-AC-0021-浦发银行五折购 ---start
        quanli_require('module/base/activity/activityPf.class.php');
        //实例建设银行五折购活动类
        $activityPf=new activityPf();
        $_REQUEST["pcustID"]=$G_SHOP->currentPCustID;
        $activity_pf=$activityPf->activity_master($_REQUEST);
        //add by 20141219 allen.qiang  FUN-AC-0021-浦发银行五折购 ---end


        //add by 20141219 sun.qiang 三八 ---start
        /*   quanli_require('module/base/activity/activityWomensday.class.php');
           //实例建设银行五折购活动类
           $activityWomensday=new activityWomensday();
           $_REQUEST["pcustID"]=$G_SHOP->currentPCustID;
           $activity_womensday=$activityWomensday->activity_master($_REQUEST);
           if($activityWomensday->buy_count($G_SHOP->currentPCustID,$activity_womensday['billPromotion']) > 0)
           {
               return array( 'code' => 404, 'message' => '您今日已购买过活动蛋糕，欢迎明日再来订购' );
           }
           if($activity_womensday['activity_num']<=0)
           {
               return array( 'code' => 404, 'message' => '活动蛋糕已售罄' );
           }*/

        //add by 20141219 sun.qiang 三八 ---end


        //实例商品配件类
        $goodsFittings = new goodsFittings( $G_SHOP->DBCA );

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        //购物车信息
        $bill_array = $this->getCartInfo('now_buy');
        $cart_array =& $bill_array['goods'];
        $cart_array['general'] = array();

        $now_buy="now_buy_activity";
        //促销渠道ID
        $activityspike=new activitySpike();
        if($type=="MS"){
            $bill_array['master']['billPromotion'] = $activityspike->billPromotion;
        }
        else if($type=="XWC"){
            $now_buy="now_buy_shengbang";
            $bill_array['master']['billPromotion'] = $activityspike->billPromotion_xwc;
        }else if($type=="LV5"){//diandong
            $now_buy="now_buy_LV5";
            $bill_array['master']['billPromotion'] = $activityspike->billPromotion_lv5;
        }else if($type=="NY_XYK"){//allen.qiang 2014-10-28 农业银行信用卡五折购 add
            $now_buy="now_buy_ny_xyk";
            $bill_array['master']['billPromotion'] = $activityspike->billPromotion_ny_xyk;
        }else if($type=="BD11"){//diandong.cao 2014-11-06 BD双11
            $now_buy="now_buy_bd11";
            $bill_array['master']['billPromotion'] = $activityspike->paymentID_11_pc;
        }else if($type=="CBC"){//add 20141121 allen.qiang FUN-AC-0020-BD建设银行五折购
            $now_buy="now_buy_cbc";
            $bill_array['master']['billPromotion'] = $activity_master["billPromotion"];
        }else if($type=="PF"){//add 20141219 allen.qiang FUN-AC-0021-浦发银行五折购
            $now_buy="now_buy_pf";
            $bill_array['master']['billPromotion'] = $activity_pf["billPromotion"];
        }
        else if($type=="christmas"){//add 20141222 diandong.cao FUN-AC-0031-BD圣诞节
            $now_buy="now_buy_christmas";
            $bill_array['master']['billPromotion'] = $config_array['configID'];
        }
        else if(strstr($type,"valentine_BD_")){//add by allen.qiang 2015-01-28 FUN-AC-0038-BD情人节活动
            $now_buy="now_buy_valentine_BD";
            $activity_config = new activityConfig("Valentine_BD"); //活动配置
            $config_array = $activity_config->config;
            $bill_array['master']['billPromotion'] = $config_array["billPromotion"];
            $bill_array['master']['birthday_card']= str_replace("valentine_BD_","爱情秘钥",$type);
        }
        /*  else if($type=="womensday")
          {
              $now_buy="now_buy_womensday";
              $bill_array['master']['billPromotion'] = $activity_womensday['billPromotion'];



          }*/
        else{
            $bill_array['master']['billPromotion']=0;
        }

        //查询商品信息
        $arg_array = array(
            'postID' => $postID
        );
        $goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );
        if (!$goods_array) {
            return array( 'code' => 404, 'message' => '商品信息没有找到' );
        }
        if ($goods_array['cityID'] != $city_id) {
            return array( 'code' => 404, 'message' => '此款产品在当前地区暂不支持配送' );
        }
        if($type!="MS" && $now_buy!="now_buy_activity"){//判断是否秒杀
            if ($goods_array['useFlg'] == 0 || $goods_array['useFlg'] == 2 || $goods_array['useFlg'] == 3) {
                return array( 'code' => 404, 'message' => '商品已下架或售罄' );
            }
        }


        //查询商品配件
        $arg_array = array(
            'postID' => $postID
        );
        $goods_fittings_array = $goodsFittings->searchGoodsFitsRelation( $arg_array );

        //购物车商品数组结构
        $goods_array['goods_amount'] = $goods_amount;
        $goods_array['fittings'] = $goods_fittings_array;
        $cart_goods_array = $this->createCartGoodsStructure( $goods_array);
        $cart_array['general'][$postID] = $cart_goods_array;

        //保存购物车信息
        $this->setCartInfo( $bill_array, $now_buy );

        $data_array = array(
            'postID' => $cart_array['general'][$postID]['postID'],
            'product_code' => $cart_array['general'][$postID]['product_code'],
            'goodsName' => $cart_array['general'][$postID]['goodsName'],
            'price' => $cart_array['general'][$postID]['price'],
            'goods_amount' => $goods_amount,
            'bigCategoryID' => $cart_array['general'][$postID]['bigCategoryID'],
            'bigCategoryName' => $cart_array['general'][$postID]['bigCategoryName']
        );

        return array( 'code' => 200, 'message' => 'ok', 'data' => $data_array );

    }

    /**
     * 循环删除购物车已购商品
     */
    public function removeIsBuyCartInfo( $cart_name = 'shop_cart' ){

        $bill_array = $this->getCartInfo( $cart_name );
        $goods_array = &$bill_array['goods']['general'];

        $this->removeCartInfo( $cart_name );

        $count = 0;

        if(!is_null($goods_array))

            foreach( $goods_array as &$goods ){

                if(isset($goods['is_buy']) && $goods['is_buy'] == 1)
                    continue;

                $result = $this->addGeneralGoods($goods['postID'], $goods['goods_amount']);

                if($result['code'] != 200)
                    continue;

                $count += $goods['goods_amount'];
            }


        $this->updateCartGoodsAmount($count);

        if($count == 0)

            $this->removeCartInfo( $cart_name );

    }

    /**
     * 更新已选购物车商品
     * @param $cart_name        购物车名称
     * @param $is_buy           是否筛选已选购买
     * @return array            结果
     */
    public function updateCartGoods( $cart_name = 'shop_cart', $is_buy = 1 ){

        global $G_SHOP;

        //加载类文件
        quanli_include('module/base/goods/quanliGoods.class.php');

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');
        $bill_array = $this->getCartInfo( $cart_name );

        $return_array = array(
            'errCount'          => 0,
            'errMessage'        => '',
            'errPostID'         => 0
        );

        $count = $this->getCartGoodsAmount();

        foreach($bill_array['goods']['general'] as &$goods){

            if($is_buy == 1 && isset($goods['is_buy']) && $goods['is_buy'] == 0)
                continue;

            $errMessage = '';

            //查询商品信息
            $arg_array = array(
                'postID' => $goods['postID']
            );

            $goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );

            if(!$goods_array)
                $errMessage = $goods_array['goodsName'] . ' 商品信息没有找到，将从订单列表中移除！';

            if ($goods_array['cityID'] != $city_id)
                $errMessage = $goods_array['goodsName'] . ' 此款产品在当前地区暂不支持配送，将从订单列表中移除！';

            if ($goods_array['useFlg'] == 0 || $goods_array['useFlg'] == 2 || $goods_array['useFlg'] == 3)
                $errMessage = '商品 ' . $goods_array['goodsName'] . ' 已下架或售罄，将从订单列表中移除！';

            if( $errMessage != ''){
                $count -= $goods['goods_amount'];
                $return_array = array(
                    'errCount'           => ++$return_array['errCount'],
                    'errMessage'        => $errMessage,
                    'errPostID'         => $goods_array['postID']
                );
            }
        }

        if($return_array['errMessage'] != '' && $return_array['errCount'] > 0){
            unset($bill_array['goods']['general'][$return_array['errPostID']]);
            $this->updateCartGoodsAmount( $count );
            $this->setCartInfo( $bill_array );
        }

        return array('code' => 200, 'message' => 'ok', 'data' => $return_array);

    }

    /**
     * 更新普通商品购物车商品磅数
     * @param int $index 商品索引
     * @param int $goods_spec 商品磅数
     * @param int $goods_amount 商品数量
     * @return array
     */
    public function updateGoodsSpec( $index, $postID, $goods_amount ) {

        //购物车信息
        $bill_array = $this->getCartInfo();

        //检查商品是否在购物车里
        if (!isset($bill_array['goods']['general'][$index])) {
            return array( 'code' => 400, 'message' => '商品信息没有找到' );
        }

        $oldPostID = $index; //更新磅数之前的商品postID

        $newPostID = $postID; //更新磅数之后的商品postID

//        $oldGoodArr = $bill_array; //更新磅数之前的购物车商品信息

        $data_array = $this->addGeneralGoods($postID, $goods_amount);

        if($data_array['code'] == 200){
            $newGoodArr = $this->getCartInfo(); //更新磅数之后的购物车商品信息

            $emptyGoodArr = array();    //定义新数组，用来存放组合后的购物车商品信息

            //更新购物车商品，商品显示顺序的调整（按原来的显示顺序显示）
            foreach($newGoodArr['goods']['general'] as $key => $val){

                if($key == $oldPostID){
                    $val = $newGoodArr['goods']['general'][$newPostID];
                    $key = $newPostID;
                }

                $emptyGoodArr['goods']['general'][$key] = $val;

            }

            //保存购物车信息
            $emptyGoodArr['change_time'] = time();
            $this->setCartInfo( $emptyGoodArr );
        }

        return array( 'code' => 200, 'message' => '商品更新成功', 'data' => $data_array );

    }

    /**
     * 多个商品加入购物车
     * @param string $postID_array 多个商品网购号以逗号拼接
     * @param string $goods_amount_array 多个商品数量以逗号拼接
     * @param string $type 活动名称
     * @return array
     * add by 20141215 allen.qiang FUN-AC-0029-姜饼项目
     */
    public function nowBuy_Multiple($postID_array, $goods_amount_array,$type) {

        global $G_SHOP;

        //加载类文件
        quanli_include('module/base/goods/quanliGoods.class.php');
        quanli_include('module/base/goods/goodsFittings.class.php');
        quanli_require('module/base/activity/activitySpike.class.php');

        //add by 20141215 allen.qiang FUN-AC-0029-姜饼项目 start
        quanli_require('module/base/activity/activityConfig.class.php');//活动配置文件
        //实例化活动配置类
        $activity_config = new activityConfig("Gingerbread"); //活动配置
        $config_array = $activity_config->config;
        //add by 20141215 allen.qiang FUN-AC-0029-姜饼项目 end

        //实例商品配件类
        $goodsFittings = new goodsFittings( $G_SHOP->DBCA );

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        //购物车信息
        $bill_array = $this->getCartInfo($type);
        $cart_array =& $bill_array['goods'];
        $cart_array['general'] = array();

        $now_buy="now_buy_Gingerbread";
        if($type=="Gingerbread"){  //add by 20141215 allen.qiang FUN-AC-0029-姜饼项目
            $bill_array['master']['billPromotion'] = $config_array["billPromotion"];
        }else if($type=="Gingerbread_Wap"){  //add by 20141217allen.qiang FUN-AC-0029-姜饼项目wap端
            $now_buy="now_buy";
        }

        $postIDarray=explode(',',$postID_array);
        $goods_amountArray=explode(',',$goods_amount_array);
        foreach($postIDarray as $index =>$postID){
            //查询商品信息
            $arg_array = array(
                'postID' => $postID
            );
            $goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );
            if (!$goods_array) {
                return array( 'code' => 404, 'message' => '商品信息没有找到' );
            }
            if ($goods_array['cityID'] != $city_id) {
                return array( 'code' => 404, 'message' => '此款产品在当前地区暂不支持配送' );
            }
            if($type!="MS"){//判断是否秒杀
                if ($goods_array['useFlg'] == 0 || $goods_array['useFlg'] == 2 || $goods_array['useFlg'] == 3) {
                    return array( 'code' => 404, 'message' => '商品已下架或售罄' );
                }
            }


            //查询商品配件
            $arg_array = array(
                'postID' => $postID
            );
            $goods_fittings_array = $goodsFittings->searchGoodsFitsRelation( $arg_array );

            //购物车商品数组结构
            $goods_array['goods_amount'] = $goods_amountArray[$index];
            $goods_array['fittings'] = $goods_fittings_array;
            $cart_goods_array = $this->createCartGoodsStructure( $goods_array);
            $cart_array['general'][$postID] = $cart_goods_array;
        }

        //保存购物车信息
        $this->setCartInfo( $bill_array, $now_buy );

        return array( 'code' => 200, 'message' => 'ok' );

    }

    /**
     * 添加商品到购物车
     * $postID 商品网购DI，$goods_amount 商品数量，$nowbuyName 购物车名称，$activityName 活动配置名称
     * @return $activity_array
     * @author  20141229allen.qiang <allen.qiang@fistforward.com>
     */
    public function activity_nowBuy($postID, $goods_amount,$nowbuyName,$activityName) {

        global $G_SHOP;

        //加载类文件
        quanli_include('module/base/goods/quanliGoods.class.php');
        quanli_include('module/base/goods/goodsFittings.class.php');
        quanli_require('module/base/activity/activityConfig.class.php');//活动配置文件

        $activity_config = new activityConfig($activityName); //活动配置
        $config_array = $activity_config->config;

        //实例商品配件类
        $goodsFittings = new goodsFittings( $G_SHOP->DBCA );

        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );

        $city_id = $G_SHOP->get_cookie_info('EBSIG_CITY_ID');

        //购物车信息
        $bill_array = $this->getCartInfo('now_buy');
        $cart_array =& $bill_array['goods'];
        $cart_array['general'] = array();

        //促销渠道ID
        $bill_array['master']['billPromotion'] = $config_array["billPromotion"];

        //查询商品信息
        $arg_array = array(
            'postID' => $postID
        );
        $goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );
        if (!$goods_array) {
            return array( 'code' => 404, 'message' => '商品信息没有找到' );
        }
        if ($goods_array['cityID'] != $city_id) {
            return array( 'code' => 404, 'message' => '此款产品在当前地区暂不支持配送' );
        }


        //查询商品配件
        $arg_array = array(
            'postID' => $postID
        );
        $goods_fittings_array = $goodsFittings->searchGoodsFitsRelation( $arg_array );

        //购物车商品数组结构
        $goods_array['goods_amount'] = $goods_amount;
        $goods_array['fittings'] = $goods_fittings_array;
        $cart_goods_array = $this->createCartGoodsStructure( $goods_array);
        $cart_array['general'][$postID] = $cart_goods_array;

        //保存购物车信息
        $this->setCartInfo( $bill_array, $nowbuyName );

        $data_array = array(
            'postID' => $cart_array['general'][$postID]['postID'],
            'product_code' => $cart_array['general'][$postID]['product_code'],
            'goodsName' => $cart_array['general'][$postID]['goodsName'],
            'price' => $cart_array['general'][$postID]['price'],
            'goods_amount' => $goods_amount,
            'bigCategoryID' => $cart_array['general'][$postID]['bigCategoryID'],
            'bigCategoryName' => $cart_array['general'][$postID]['bigCategoryName']
        );

        return array( 'code' => 200, 'message' => 'ok', 'data' => $data_array );

    }

}
